﻿using Modles.DTO;
using Modles.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BilbilDAL
{
    public class CarouselDAL
    {
        /// <summary>
        /// 打印轮播图表按id降序排序
        /// </summary>
        /// <returns></returns>
        public List<Carousel_DTO> GetCarousel()
        {
            using (VideoPlatformDbContext db = new VideoPlatformDbContext())
            {
                IQueryable<Carousel_DTO> query = from a in db.Carousels
                                                 join b in db.Images on a.ImageId equals b.Id
                                                 select new Carousel_DTO {
                                                     Id = a.Id,
                                                     ImageId = a.ImageId,
                                                     Address = b.Address,
                                                     Description = b.Description
                                                 };
                return query.ToList();
            }
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="ImageId"></param>
        /// <returns></returns>
        public bool AddCarousel(int ImageId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                com.Carousels.Add(new Carousel() {ImageId = ImageId});
                if (com.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeleteCarousel(int Id)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                Carousel carousel = com.Carousels.Find(Id);
                if (carousel == null) { return false; }
                com.Carousels.Remove(carousel);
                if (com.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
